#!/bin/bash
# fprintd 开发环境验证脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 检查结果计数
PASSED=0
FAILED=0
WARNINGS=0

# 检查函数
check_command() {
    local cmd=$1
    local desc=$2
    
    if command -v $cmd >/dev/null 2>&1; then
        echo -e "${GREEN}[PASS]${NC} $desc ($cmd 已安装)"
        ((PASSED++))
    else
        echo -e "${RED}[FAIL]${NC} $desc ($cmd 未找到)"
        ((FAILED++))
    fi
}

check_file() {
    local file=$1
    local desc=$2
    
    if [ -f "$file" ]; then
        echo -e "${GREEN}[PASS]${NC} $desc ($file 存在)"
        ((PASSED++))
    else
        echo -e "${RED}[FAIL]${NC} $desc ($file 不存在)"
        ((FAILED++))
    fi
}

check_package() {
    local pkg=$1
    local desc=$2
    
    if pkg-config --exists $pkg 2>/dev/null; then
        local version=$(pkg-config --modversion $pkg)
        echo -e "${GREEN}[PASS]${NC} $desc ($pkg $version)"
        ((PASSED++))
    else
        echo -e "${RED}[FAIL]${NC} $desc ($pkg 未找到)"
        ((FAILED++))
    fi
}

check_group() {
    local group=$1
    local desc=$2
    
    if groups | grep -q $group; then
        echo -e "${GREEN}[PASS]${NC} $desc (用户在 $group 组中)"
        ((PASSED++))
    else
        echo -e "${YELLOW}[WARN]${NC} $desc (用户不在 $group 组中)"
        ((WARNINGS++))
    fi
}

echo "fprintd 开发环境验证"
echo "===================="

echo -e "\n检查基本构建工具:"
check_command "gcc" "C 编译器"
check_command "meson" "Meson 构建系统"
check_command "ninja" "Ninja 构建工具"
check_command "pkg-config" "pkg-config 工具"

echo -e "\n检查开发库:"
check_package "glib-2.0" "GLib 开发库"
check_package "libfprint-2" "libfprint 开发库"
check_package "libpam" "PAM 开发库"
check_package "libsystemd" "systemd 开发库"
check_package "dbus-1" "D-Bus 开发库"
check_package "polkit-gobject-1" "Polkit 开发库"

echo -e "\n检查开发工具:"
check_command "python3" "Python 3"
check_command "gettext" "Gettext 工具"
check_command "gdb" "GDB 调试器"
check_command "valgrind" "Valgrind 内存检查工具"
check_command "strace" "strace 系统调用跟踪"

echo -e "\n检查权限设置:"
check_group "plugdev" "设备访问权限"

echo -e "\n检查项目文件:"
check_file "meson.build" "Meson 构建文件"
check_file "src/main.c" "主程序源码"
check_file "pam/pam_fprintd.c" "PAM 模块源码"

echo -e "\n检查构建目录:"
if [ -d "builddir" ]; then
    echo -e "${GREEN}[PASS]${NC} 构建目录存在"
    ((PASSED++))
    
    if [ -f "builddir/build.ninja" ]; then
        echo -e "${GREEN}[PASS]${NC} 构建已配置"
        ((PASSED++))
    else
        echo -e "${YELLOW}[WARN]${NC} 构建未配置，运行 'meson setup builddir'"
        ((WARNINGS++))
    fi
    
    if [ -f "builddir/src/fprintd" ]; then
        echo -e "${GREEN}[PASS]${NC} 主程序已编译"
        ((PASSED++))
    else
        echo -e "${YELLOW}[WARN]${NC} 主程序未编译，运行 'meson compile -C builddir'"
        ((WARNINGS++))
    fi
else
    echo -e "${YELLOW}[WARN]${NC} 构建目录不存在，运行 'meson setup builddir'"
    ((WARNINGS++))
fi

echo -e "\n检查系统服务:"
if systemctl is-enabled fprintd.service >/dev/null 2>&1; then
    echo -e "${GREEN}[PASS]${NC} fprintd 服务已启用"
    ((PASSED++))
else
    echo -e "${YELLOW}[WARN]${NC} fprintd 服务未启用"
    ((WARNINGS++))
fi

if systemctl is-active fprintd.service >/dev/null 2>&1; then
    echo -e "${GREEN}[PASS]${NC} fprintd 服务正在运行"
    ((PASSED++))
else
    echo -e "${YELLOW}[WARN]${NC} fprintd 服务未运行"
    ((WARNINGS++))
fi

echo -e "\n验证结果总结:"
echo "=============="
echo -e "${GREEN}通过: $PASSED${NC}"
echo -e "${YELLOW}警告: $WARNINGS${NC}"
echo -e "${RED}失败: $FAILED${NC}"

if [ $FAILED -gt 0 ]; then
    echo -e "\n${RED}发现问题！${NC}请安装缺失的依赖。"
    echo "可以运行 './scripts/setup-dev-env.sh' 自动安装依赖。"
    exit 1
elif [ $WARNINGS -gt 0 ]; then
    echo -e "\n${YELLOW}发现警告。${NC}开发环境基本可用，但建议解决警告项。"
    exit 0
else
    echo -e "\n${GREEN}开发环境完全就绪！${NC}"
    exit 0
fi
